import axios from "axios";
import type { Method } from "axios";
interface ResponseType<T> {
  success: boolean;
  data: T;
  message: string;
}

type OptionProp = {
  methods?: Method;
  data?: any;
};

export async function useRequest<T>(
  url: string,
  options?: OptionProp
): Promise<ResponseType<T>> {
  return new Promise((resolve, reject) => {
    const cookie = useCookie('UserSource')
    axios(url, {
      baseURL: appConfig.ip,
      method: options?.methods || "get",
      data: options?.data || {},
      headers: {
        "Content-Type": "application/json",
        "Authorization": cookie.value
      },
    })
      .then((res) => {
       
        resolve({
          success: true,
          data: (res.data && res.data.data) as T,
          message: "success",
        });
      })
      .catch((err) => {
        resolve({
          success: false,
          data: "" as T,
          message: err,
        });
      });
  });
}
